
import { userLogin } from "@/utils/api/userApi";
import { useState } from "react";
import { useDispatch } from "react-redux";
import {set_user_info} from'@/store/slice/other/user'
import { useNavigate } from "react-router-dom";

function Login() {
   const dispatch= useDispatch()
   const navigate=useNavigate()
    const [userInfo, setUserInfo] = useState({
        phone: '',
        code: ''
    })

    const submit = async() => {
        console.log(userInfo);
        let temp=await userLogin(userInfo)
        console.log(temp);
        if(temp.data.success){
            dispatch(set_user_info(temp.data.data)) 
            sessionStorage.setItem("token",temp.data.data.token)
            navigate('/home')
        }
        
    }
    type userInfo = {
        phone: string ,
        code: string 
    }
    const changeForm = (e: any, last: userInfo, tag: string) => {
        let obj:userInfo ={
            ...last,
            [tag]: e.target.value
        }

        setUserInfo(obj)

    }
    return (
        <>
            手机号<input type="text" name="" id="" value={userInfo.phone} onChange={(e: any) => { changeForm(e, userInfo, 'phone') }} />
            验证码<input type="text" name="" id="" value={userInfo.code} onChange={(e: any) => { changeForm(e, userInfo, 'code') }} />
            <button onClick={submit}> 登录</button>

        </>
    );
}

export default Login;
